package org.example.medical.controller;

import org.example.medical.entity.DiagnosisRecord;
import org.example.medical.service.DiagnosisRecordService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/diagnosisRecords")
public class DiagnosisRecordApiController {
    @Autowired
    private DiagnosisRecordService diagnosisRecordService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<DiagnosisRecord>> listDiagnosisRecords(
            @RequestParam(required = false) String patientName,
            @RequestParam(required = false) String diseaseName,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String diagnosisTime,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<DiagnosisRecord> records = diagnosisRecordService.getDiagnosisRecords(
                patientName,
                diseaseName,
                status,
                diagnosisTime,
                page,
                size
        );
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增诊断记录
    @PostMapping
    public ApiResult<?> addDiagnosisRecord(@RequestBody DiagnosisRecord diagnosisRecord) {
        diagnosisRecordService.addDiagnosisRecord(diagnosisRecord);
        return ApiResult.success(null, "Diagnosis record added successfully");
    }

    // 3. 更新诊断记录
    @PutMapping("/{diagnosisId}")
    public ApiResult<?> updateDiagnosisRecord(
            @PathVariable Long diagnosisId,
            @RequestBody DiagnosisRecord diagnosisRecord
    ) {
        diagnosisRecord.setDiagnosisId(diagnosisId);
        diagnosisRecordService.updateDiagnosisRecord(diagnosisRecord);
        return ApiResult.success(null, "Diagnosis record updated successfully");
    }

    // 4. 软删除诊断记录
    @DeleteMapping("/{diagnosisId}")
    public ApiResult<?> softDeleteDiagnosisRecord(@PathVariable Long diagnosisId) {
        diagnosisRecordService.softDeleteDiagnosisRecord(diagnosisId);
        return ApiResult.success(null, "Diagnosis record deleted successfully");
    }

    // 5. 批量软删除诊断记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteDiagnosisRecords(@RequestBody List<Long> diagnosisIds) {
        diagnosisRecordService.batchSoftDeleteDiagnosisRecords(diagnosisIds);
        return ApiResult.success(null, "Diagnosis records deleted successfully");
    }
}